<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/jsp/common/common_include_for_server.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<title>用户管理</title>
		<meta http-equiv="pragma" content="no-cache" />
		<meta http-equiv="cache-control" content="no-cache" />
		<meta http-equiv="expires" content="0" />
		<%@ include file="/jsp/common/common_include_for_client.jsp"%>
		<script type="text/javascript">
		<!--
			var saveUserDialog = null;
			var updatePwdDialog = null;
			var configAuthorityDialog = null;
			/* onload init method start.
			**************************************************************/
			$(function(){
				// init save user dialog:
				saveUserDialog = $('#divSaveUserDialog').dialog({
					title: '用户信息',
					autoOpen: false, 
					resizable: false,
					modal: true,
					open: function(event, ui) {
						var uid = $('#saveUserForm input[name="user.userId"]').val();
						if(uid.length == 0){
							saveUserDialog.dialog('option', 'title', '添加新用户');
						}else{
							saveUserDialog.dialog('option', 'title', '修改用户信息');
						}
					},
					buttons: {
						"确定":function(){
							$.post('<%=request.getContextPath() %>/manage/userAction!saveUserDo.action', 
								$('#saveUserForm').serialize(),
								function(){
									saveUserDialog.dialog('close');
									$('#query_form').get(0).reset();
									$('#query_form').submit();
							});
							
						},
						"取消":function(){ saveUserDialog.dialog('close'); }
					}
				});
			
				// init update user password dialog:
				updatePwdDialog = $('#divUpdatePwdDialog').dialog({
					title: '设置用户登录密码',
					autoOpen: false, 
					resizable: false,
					modal: true,
					buttons: {
						"确定":function(){
							$.post('<%=request.getContextPath() %>/manage/userAction!updatePasswordDo.action', 
								$('#updatePwdForm').serialize(),
								function(data){
									alert('密码设置成功！');
									updatePwdDialog.dialog('close');
							});
						},
						"取消":function(){ updatePwdDialog.dialog('close'); }
					}
				});
			
				//configAuthorityDialog
				configAuthorityDialog = $('#divConfigAuthorityDialog').dialog({
					title: '设置用户角色',
					autoOpen: false, 
					resizable: false,
					modal: true,
					buttons: {
						"确定":function(){
							$.post('<%=request.getContextPath() %>/manage/userAction!configUserAuthorityDo.action', 
								$('#authorityForm').serialize(),
								function(data){
									configAuthorityDialog.dialog('close');
									$('#query_form').get(0).reset();
									$('#query_form').submit();
							});
						},
						"取消":function(){ configAuthorityDialog.dialog('close'); }
					}
				});
			});
			
			/* onload init method end.
			**************************************************************/
			function initDialog4AddUser(){
				$('#saveUserForm input[name="user.userId"]').val('');
				saveUserDialog.dialog('open');
			}
			function initDialog4SaveUser(uid){
				$.post('<%=request.getContextPath()%>/manage/userAction!userDetail.action?user.userId='+ uid, function(data){
					if(typeof(data)==='object'){
						$('#saveUserForm input[name="user.userId"]').val(data.user.userId);
						$('#saveUserForm input[name="user.loginName"]').val(data.user.loginName).attr('readonly', true);
						$('#saveUserForm input[name="user.realName"]').val(data.user.realName);
						saveUserDialog.dialog('open');
					}
				});
			}
			function initDialog4UpdatePwd(userId, loginName, realName){
				$('#updatePwdForm input[name="user.userId"]').val(userId);
				$('#updatePwdForm span.user_login_name').text(loginName);
				$('#updatePwdForm span.user_real_name').text(realName);
				$('#updatePwdForm input[name="user.loginPwd"]').val('');
				updatePwdDialog.dialog('open');
			}
			function initDialog4ConfigAuthority(userId){
				$.post('<%=request.getContextPath()%>/manage/userAction!fnidRoleIdsByUser.action?user.userId='+ userId, function(data){
					if(typeof(data)==='object'){
						$('#authorityForm input[name="user.userId"]').val(userId);
						$('#authorityForm span.user_login_name').text( $('#user_tr_'+ userId +' input[name="loginName"]' ).val());
						$('#authorityForm span.user_real_name').text(  $('#user_tr_'+ userId +' input[name="realName"]'  ).val());
						var userRoleIds = data.roleId;
						$('#authorityForm input[name="roleId"]').attr('checked', false);
						if(userRoleIds && userRoleIds.length>0){
							for(var i=0; i<userRoleIds.length; i++){
								$('#authorityForm input[value="'+ userRoleIds[i] +'"]').attr('checked', true);
							}
						}
						configAuthorityDialog.dialog('open');
					}
				});
			}
			function deleteUser(userId){
				if(confirm('确定删除该用户？')){
					$('#query_form input[name="user.userId"]').val(userId);
					$('#query_form').attr('action', '<%=request.getContextPath()%>/manage/userAction!deleteUserDo.action');
					$('#query_form').submit();
				}
			}
		//-->
		</script>
	</head>

	<body>
	<!-- Dialogs start -->
	<div id="divSaveUserDialog" style="display: none;">
	    <s:form id="saveUserForm">
	    	<input type="hidden" name="user.userId"/>
	    	登录名称：<input type="text" name="user.loginName" /><br/>
	    	真实姓名：<input type="text" name="user.realName" />
	    </s:form>
	</div>
	<div id="divUpdatePwdDialog" style="display: none;">
		<form id="updatePwdForm">
			<input type="hidden" name="user.userId"/>
	    	登录名称：<span class="user_login_name"><s:property value="user.loginName"/></span><br/>
	    	真实姓名：<span class="user_real_name"><s:property value="user.realName"/></span><br/>
	    	新密码：<input type="password" name="user.loginPwd"/>
		</form>
	</div>
	<div id="divConfigAuthorityDialog" style="display: none;">
		<form id="authorityForm">
			<input type="hidden" name="user.userId"/>
	    	登录名称：<span class="user_login_name"><s:property value="user.loginName"/></span><br/>
	    	真实姓名：<span class="user_real_name"><s:property value="user.realName"/></span><br/>
			<div>
				<s:iterator value="roleList" var="role">
					<div><input id="roleId_${role.roleId}" type="checkbox" name="roleId" value="${role.roleId}"/><label for="roleId_${role.roleId}"><s:property value="roleName"/></label></div>
				</s:iterator>
			</div>
		</form>
	</div>
	<!-- Dialogs end -->
	<input type="button" value="添加用户" onclick="initDialog4AddUser();"/>
	<form id="query_form" action="<%=request.getContextPath() %>/manage/userAction!userList.action" method="post">
		<s:hidden name="user.userId"></s:hidden>
		<s:hidden name="pageUtil.showPage" value="%{pageUtil.showPage}" id="show_page"></s:hidden>
		登录名：<s:textfield name="user.loginName"></s:textfield>
		真实姓名：<s:textfield name="user.realName"></s:textfield>
		<input type="button" value="查询" onclick="go(1, true)" />
	</form>
	<table border="1">
		<tr>
			<th>ID</th>
			<th>登录名</th>
			<th>真实姓名</th>
			<th>角色</th>
			<th>注册时间</th>
			<th>操作</th>
		</tr>
		<s:iterator value="pageUtil.pageData">
			<tr id="user_tr_<s:property value="userId"/>">
				<td><s:property value="userId"/></td>
				<td><s:property value="loginName"/></td>
				<td><s:property value="realName"/></td>
				<td>
					<s:iterator value="userRoles">
						<div><s:property value="role.roleName"/></div>
					</s:iterator>
				</td>
				<td><s:date name="insertTime" format="yyyy-MM-dd HH:mm"/></td>
				<td>
					<a href="javascript:initDialog4ConfigAuthority('<s:property value='userId'/>', '<s:property value='loginName'/>', '<s:property value='realName'/>');">分配角色</a>
					<a href="javascript:initDialog4SaveUser(<s:property value='userId'/>);">修改信息</a>
					<a href="javascript:initDialog4UpdatePwd('<s:property value='userId'/>', '<s:property value='loginName'/>', '<s:property value='realName'/>');">设置密码</a>
					<a href="javascript:deleteUser(<s:property value='userId'/>)">删除</a>
					<s:hidden name="loginName" value="%{loginName}"/>
					<s:hidden name="realName" value="%{realName}"/>
					<s:hidden name="roleId" value="%{roleId}"/>
				</td>
			</tr>
		</s:iterator>
	</table>
	<script type="text/javascript">
	<!--
		function go(showPage, newSearch){
			$('#show_page').val(showPage);
			if(!newSearch){
				$('#query_form').get(0).reset();
			}
			$('#query_form').submit();
		}
	//-->
	</script>
	<%@ include file="/jsp/common/common_include_for_pages.jsp" %>
	</body>
</html>
